/*
 *    Copyright (c) 2018-2025, lengleng All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions are met:
 *
 * Redistributions of source code must retain the above copyright notice,
 * this list of conditions and the following disclaimer.
 * Redistributions in binary form must reproduce the above copyright
 * notice, this list of conditions and the following disclaimer in the
 * documentation and/or other materials provided with the distribution.
 * Neither the name of the pig4cloud.com developer nor the names of its
 * contributors may be used to endorse or promote products derived from
 * this software without specific prior written permission.
 * Author: lengleng (wangiegie@gmail.com)
 */
package com.pig4cloud.pigx.shop.user.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.pig4cloud.pigx.common.core.constant.enums.ShopUserTypeEnum;
import com.pig4cloud.pigx.shop.user.api.entity.PromotionChannel;
import com.pig4cloud.pigx.shop.user.api.entity.ShopUser;
import com.pig4cloud.pigx.shop.user.api.vo.PromotionChannelVO;
import com.pig4cloud.pigx.shop.user.mapper.PromotionChannelMapper;
import com.pig4cloud.pigx.shop.user.service.PromotionChannelService;
import com.pig4cloud.pigx.shop.user.service.ShopUserService;
import lombok.AllArgsConstructor;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

/**
 * 推广渠道管理
 *
 * @author pigx code generator
 * @date 2023-11-22 23:16:49
 */
@Service
@AllArgsConstructor
public class PromotionChannelServiceImpl extends ServiceImpl<PromotionChannelMapper, PromotionChannel>
		implements PromotionChannelService {

	private final ShopUserService shopUserService;

	/**
	 * 新增渠道
	 * @param promotionChannelVO
	 * @return
	 */
	@Override
	@Transactional(rollbackFor = Exception.class)
	public boolean add(PromotionChannelVO promotionChannelVO) {
		PromotionChannel promotionChannel = new PromotionChannel();
		BeanUtils.copyProperties(promotionChannelVO, promotionChannel);

		this.save(promotionChannel);

		// 新增渠道默认用户
		ShopUser shopUser = new ShopUser();
		shopUser.setLoginAccount(promotionChannelVO.getChannelCode());
		shopUser.setRealName(promotionChannelVO.getChannelName());
		shopUser.setNickName(promotionChannelVO.getChannelName());
		shopUser.setChannelCode(promotionChannel.getChannelCode());
		shopUser.setInvitedCode(promotionChannelVO.getInvitedCode());
		shopUser.setShopUserType(ShopUserTypeEnum.PLATFORM.getType());
		shopUserService.save(shopUser);

		Long shopUserId = shopUser.getId();
		shopUser.setPpath("/" + shopUserId.toString());
		shopUserService.updateById(shopUser);

		return true;
	}

	/**
	 * 获取渠道详情
	 * @param id
	 * @return
	 */
	@Override
	public PromotionChannelVO getChannelInfo(Long id) {
		PromotionChannelVO result = new PromotionChannelVO();
		PromotionChannel channel = this.getById(id);
		BeanUtils.copyProperties(channel, result);

		LambdaQueryWrapper<ShopUser> lqw = new LambdaQueryWrapper<>();
		lqw.eq(ShopUser::getLoginAccount, result.getChannelCode());
		lqw.eq(ShopUser::getNickName, result.getChannelName());
		lqw.eq(ShopUser::getRealName, result.getChannelName());
		ShopUser shopUser = shopUserService.getOne(lqw, false);
		result.setInvitedCode(shopUser.getInvitedCode());

		return result;
	}

}
